const fs = require('fs');
const ph = require('path');
const config = require('_/config');

/** @name 文件上传 **/
exports.upload = async ctx => {
	const { filename } = ctx.req.file
	const url = ctx.req.url.substr(6)
  ctx.success({ path: url + '/' + filename }, '上传成功');
};

/** @name 文件删除 **/
exports.delete = async ctx => {
  if(ctx.requiredParams(['path'])) return;
  const { path }  = ctx.request.body;
  const fullPath = config.rootPath + '\\public' + path;
  if(fs.existsSync(fullPath)) {
    fs.unlinkSync(fullPath);
    ctx.success('文件删除成功');
  }else {
    ctx.success('文件路径不存在');
  }
};

// 删除-工具
exports.deleteTool = (path) => {
  return new Promise((resolve, reject) => {
    let fullPath = ph.join(config.rootPath, 'public', path);
    if(fs.existsSync(fullPath)) {
      fs.unlinkSync(fullPath);
      resolve('文件删除成功')
    }else {
      reject('文件路径不存在');
    }
  })

};
